using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VoxelEngine;

namespace VoxelGame.Character
{
	public class CharacterTool : MonoBehaviour
	{
		[SerializeField]
		protected CharacterToolsController.ToolData.Tools _type;

		[SerializeField]
		protected Animation _animation;

		[SerializeField]
		protected ParticleSystem _particle;

		[SerializeField]
		protected string _idleAnimation = "idle";

		[SerializeField]
		protected string _interactAnimation = "interact";

		[SerializeField]
		protected float _animPercentToPlayBetweenActions = 0.4f;

		[SerializeField]
		protected bool _canCreateBlock;

		[SerializeField]
		protected bool _canDestroyBlock;

		[SerializeField]
		protected bool _canContinuousDestroy;

		[SerializeField]
		protected bool _needTargetBlockBeforeStart = true;

		[SerializeField]
		protected bool _canShootProjectile;

		[SerializeField]
		protected float _range;

		protected CharacterSoundController _soundController;

		protected bool _hasIdleAnimation;

		protected bool _hasOperationStarted;

		public bool CanCreateBlock => _canCreateBlock;

		public bool CanDestroyBlock => _canDestroyBlock;

		public bool CanShootProjectile => _canShootProjectile;

		public float Range => _range;

		public CharacterToolsController.ToolData.Tools ToolType => _type;

		public virtual void Setup(CharacterSoundController soundController)
		{
			_soundController = soundController;
		}

		protected virtual void Awake()
		{
			_hasIdleAnimation = (_animation[_idleAnimation] != null);
		}

		private void Update()
		{
			if (_hasIdleAnimation && !_animation.isPlaying)
			{
				_animation.CrossFade(_idleAnimation);
			}
		}

		public bool CanContinuousDestroy()
		{
			return _canContinuousDestroy;
		}

		public bool NeedValidBlockTargetBeforeStart()
		{
			return _needTargetBlockBeforeStart;
		}

		public virtual bool CreateBlock(VoxelEngine.World world, Block selectedBlock, BlockType blockType)
		{
			return false;
		}

		public virtual IEnumerator DestroyBlock(VoxelEngine.World world, Block selectedBlock, Action<Dictionary<BlockType, int>> onBlocksDestroyed, byte blockLight = 0)
		{
			yield return null;
		}

		public virtual bool CanStartOperation(bool waitHalfAnimDuration = false)
		{
			if (_hasIdleAnimation && _animation.isPlaying && _animation[_idleAnimation].enabled)
			{
				return true;
			}
			if (waitHalfAnimDuration)
			{
				return _animation.isPlaying && _animation[_interactAnimation].normalizedTime >= _animPercentToPlayBetweenActions;
			}
			return !_animation.isPlaying;
		}

		public float AnimationProgress()
		{
			return _animation[_interactAnimation].normalizedTime;
		}

		public virtual void PlayLongPressBeginAnimation()
		{
		}

		public virtual void StopLongPressBeginAnimation()
		{
		}

		public virtual void Play()
		{
			_animation.Stop();
			_animation.Play(_interactAnimation);
			_particle.Play();
		}

		public void Stop()
		{
			_animation.Stop();
			_particle.Stop();
		}
	}
}
